// pages/search_lxj/search_lxj.js
const db_lxj = wx.cloud.database()
Page({
  data: {
    totalMoney:0,
    totalNum: 0,
    cartList: [],
  },
  onLoad(options) {
    console.log("options",options);
    // let cartList = wx.getStorageSync('cart') || []
    // console.log('本地缓存的购物车数据',cartList);
    // this.setData({
    //   cartList:cartList
    // })
    // this.getTotal()
  },
  onShow(){
    let cartList = wx.getStorageSync('cart') || []
    //console.log('本地缓存的购物车数据',cartList);
    this.setData({
      cartList:cartList
    })
    this.getTotal()
  },
    //计算总价格和总数量
    getTotal(){
      let cartList = this.data.cartList
      let totalMoney = 0
      let totalNum = 0
      cartList.forEach(item=>{
        totalMoney+=item.price*item.num
        totalNum+=item.num
      })
      this.setData({
        totalMoney,
        totalNum
      })
    },
    //点击增减菜品
    subNum(e){
      let id = e.currentTarget.dataset.id
      //console.log('减',id);
      let  cartList = this.data.cartList
      cartList.forEach((item,index)=>{
       // console.log('便利了',item.name);
        if(item._id == id){
          if(item.num > 0)
          {
            item.num --       
              // console.log('点击的菜品是否在购物车里',index);
              if(item.num == 0){  //当菜品数量为0时，移除
                cartList.splice(index,1) //删除下标为index的元素
              }
          }
          else{
            wx.showToast({
              icon:'none',
              title: '已经不能再减啦！',
            })
          }
        }
      })
      console.log('当前菜品列cartList',cartList)
      this.setData({
        cartList,
      })
      this.getTotal()
      wx.setStorageSync('cart', cartList)
      console.log('购物车',cartList);
    },
    addNum(e){
      let id = e.currentTarget.dataset.id
      console.log('加',id);
      let  cartList = this.data.cartList
      cartList.forEach(item=>{
        // console.log('便利了',item.name);
        if(item._id == id){
          item.num ++; 
          if(cartList && cartList.length > 0){
            var result = cartList.find(cart=>{   //find方法 查询购物车数组cartList里面是否有当前点击的菜品
              return cart._id == id;
            })
            console.log('点击的菜品是否在购物车里',result);
            if(result){
              result.num = item.num
            }else{
              cartList.push(item)
            }
          }else{
            cartList.push(item)
          }
        }
      })
      console.log('购物车',cartList);
      this.setData({
        cartList,
      })
      this.getTotal()
       wx.setStorageSync('cart', cartList)
    },
    //清空购物车
    clearCart(){
      let foodList = this.data.foodList
      foodList.forEach(item=>{
        item.num = 0
      })
      this.setData({
        foodList,
        cartList:[],
        totalMoney:0,
        totalNum:0
      })
      wx.setStorageSync('cart', null)
    },
    //删除购物车中某一项
    deleteItem(e){
      console.log(e.currentTarget.dataset.index);
      let index = e.currentTarget.dataset.index
      let cartList = this.data.cartList
      cartList.forEach(item=>{
        if(cartList[index]._id == item._id){
          item.num = 0
        }
      })
      cartList.splice('index',1)
      this.setData({
        cartList,
      })
      this.getTotal()
      wx.setStorageSync('cart', cartList)
    },
    //去支付
    goPay(){
      wx.navigateTo({
        url: '/pages/pay/pay',
        
      })
    }
   
})